import {useCreateModal} from "@/hooks"
import EditModal from './edit-modal.vue'
import {cipherDelete} from "@/api/strategy/cipher"
import {createVNode} from "vue"
import span from "@/utils/span-solt.vue"
import {Modal, message} from "ant-design-vue"
import {QuestionCircleOutlined} from "@ant-design/icons-vue"
import {checkStatus} from "@/utils/http/axios/checkStatus"

const privateFun = function (record){
    let time = ''
    if(0<record.mintime){
        time = time+'大于'+record.mintime+'天'
    }
    if(0<record.maxtime){
        time = time+'小于'+record.maxtime+'天'
    }
    if(''==time){
        time = '任意'
    }else if(record.mintime==record.maxtime){
        time = record.mintime+'天'
    }
    return time
}

export const columns = [ // 进程策略
    {
        title: '策略编号',
        dataIndex: 'cipherno',
    },
    {
        title: '策略名称',
        dataIndex: 'name',
    },
    {
        title: '复杂加密',
        dataIndex: 'complicated',
        slots:{
            customRender: 'complicated'
        },
        slotsType: 'format',
        slotsFunc: (val) => ({
            0:'否',
            1:'是'
        }[val])
    },
    {
        title: '密码最小长度',
        dataIndex: 'minlength',
    },
    {
        title: '使用期限',
        dataIndex: 'days',
        slots:{
            customRender: 'days'
        },
        slotsType: 'component',
        slotsFunc: (record) => createVNode(span,{text:privateFun(record)})
    },
    // {
    //     title: '策略模板',
    //     dataIndex: 'tempname',
    // },
    {
        title: '操作',
        dataIndex: 'action',
        width: 200,
        slots: {
            customRender: 'action'
        },
        actions: [
            {
                type: 'popconfirm', // 控制类型，默认为a,可选： select | button | text
                key: 'id', // 删除的依据，如果需要根据多个字段删除，则字段之间以英文逗号分隔开，例如： id, name
                text: '删除',
                permission: 'cipher:delete',
                props: {
                  type: 'danger'
                },
                func: ({record}, callback) => {
                    cipherDelete({ids: [record.cipherid],force:0},{isShowMessage:false}).then((res)=>{
                            if(-5==Number(res.code)){
                                Modal.confirm({
                                    title: '提示',
                                    icon: createVNode(QuestionCircleOutlined),
                                    content: '策略已在模板中使用，是否解除所有绑定并删除',
                                    onOk:  async () => {
                                        await cipherDelete({ids:[record.cipherid],force:1},{isShowMessage: true})
                                        callback()
                                    }
                                })
                            }
                            else{
                                if(0==Number(res.code)){
                                    message.success(res.message)
                                }
                                else {
                                    checkStatus(res.code,res.message,true)
                                }
                                callback()
                                return Promise.resolve()
                            }
                        })
                },
            },
            {
                type: 'button', // 控制类型，默认为a,可选： select | button | text
                key: 'id', // 删除的依据，如果需要根据多个字段删除，则字段之间以英文逗号分隔开，例如： id, name
                text: '编辑',
                permission: 'cipher:update',
                props: {
                    type: 'warning'
                },
                func: ({record}, callback) => useCreateModal(EditModal, {
                    fields: record,
                    callback
                }),
            }
        ]
    },
]
